package org.jeecg.modules.doctor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorCertification;
import org.jeecg.common.modules.doctor.service.IDoctorCertificationService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.doctor.service.impl.DoctorCertificationServiceImpl;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.ImageUrl;
import org.jeecg.modules.system.vo.DoctorResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: doctor_certification
 * @Author: jeecg-boot
 * @Date:   2020-08-20
 * @Version: V1.0
 */
@Api(tags="doctor_certification")
@RestController
@RequestMapping("/doctor/doctorCertification")
@Slf4j
public class DoctorCertificationController extends JeecgController<DoctorCertification, IDoctorCertificationService> {
	@Autowired
	private IDoctorService doctorService;

	@Autowired
	private IDoctorCertificationService doctorCertificationService;

	@Resource
	private DoctorCertificationServiceImpl doctCertService;

	/**
	 * 分页列表查询
	 *
	 * @param doctorCertification
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "doctor_certification-分页列表查询")
	@ApiOperation(value="doctor_certification-分页列表查询", notes="doctor_certification-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(DoctorCertification doctorCertification,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DoctorCertification> queryWrapper = QueryGenerator.initQueryWrapper(doctorCertification, req.getParameterMap());
		Page<DoctorCertification> page = new Page<DoctorCertification>(pageNo, pageSize);
		IPage<DoctorCertification> pageList = doctorCertificationService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param doctorCertification
	 * @return
	 */
	@AutoLog(value = "doctor_certification-添加")
	@ApiOperation(value="doctor_certification-添加", notes="doctor_certification-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DoctorCertification doctorCertification) {
		doctorCertificationService.save(doctorCertification);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param doctorCertification
	 * @return
	 */
	@AutoLog(value = "doctor_certification-编辑")
	@ApiOperation(value="doctor_certification-编辑", notes="doctor_certification-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DoctorCertification doctorCertification) {
		//todo 认证
//		doctorCertificationService.updateById(doctorCertification);
//		doctorCertificationService.audit(doctorCertification.getId(),null,doctorCertification.getcer);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_certification-通过id删除")
	@ApiOperation(value="doctor_certification-通过id删除", notes="doctor_certification-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		doctorCertificationService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "doctor_certification-批量删除")
	@ApiOperation(value="doctor_certification-批量删除", notes="doctor_certification-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.doctorCertificationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_certification-通过id查询")
	@ApiOperation(value="doctor_certification-通过id查询", notes="doctor_certification-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DoctorCertification doctorCertification = doctorCertificationService.getById(id);
		if(doctorCertification==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(doctorCertification);
	}

	/**
	 * 通过doctorId查询医生信息（含认证信息）
	 *
	 * @param doctorId
	 * @return
	 */
	@AutoLog(value = "doctor_certification-通过doctor_id查询")
	@ApiOperation(value="doctor_certification-通过doctor_id查询", notes="doctor_certification-通过doctor_id查询")
	@GetMapping(value = "/queryByDoctorId")
	public Result<?>  queryByDoctorId(@RequestParam(name="doctor_id",required=true) String doctorId) {
		Doctor doctor = doctorService.getById(doctorId);

		DoctorCertification doctCert = doctCertService.getByDoctorId(doctorId);
		if(doctCert == null){
//			return Result.error("医生未上传资质信息");
			DoctorCertification doctCertTemp = new DoctorCertification();
			doctCertTemp.setCreateTime(null);
			doctCertTemp.setLicense1(null);
			doctCertTemp.setLicense2(null);
			doctCertTemp.setQualification1(null);
			doctCertTemp.setQualification2(null);
			doctCertTemp.setTechnology1(null);
			doctCertTemp.setTechnology2(null);
			doctCert = doctCertTemp;
		}

		DoctorResponseVo vo = this.initDoctorResponseVo(doctor,doctCert);
		return Result.ok(vo);
	}

	/**
	 * 组合医生信息和医生认证信息数据
	 * @param doctor
	 * @param doctorCertification
	 * @return
	 */
	private DoctorResponseVo initDoctorResponseVo(Doctor doctor ,DoctorCertification doctorCertification){
		DoctorResponseVo vo = new DoctorResponseVo();

		vo.setId(doctor.getId());
		vo.setName(doctor.getName());
		vo.setMobile(doctor.getMobile());
		vo.setCreateBy(doctor.getCreateBy());
		vo.setCreateTime(doctorCertification.getCreateTime());

		vo.setLicense1(ImageUrl.handle(doctorCertification.getLicense1()));
		vo.setLicense2(ImageUrl.handle(doctorCertification.getLicense2()));
		vo.setQualification1(ImageUrl.handle(doctorCertification.getQualification1()));
		vo.setQualification2(ImageUrl.handle(doctorCertification.getQualification2()));
		vo.setTechnology1(ImageUrl.handle(doctorCertification.getTechnology1()));
		vo.setTechnology2(ImageUrl.handle(doctorCertification.getTechnology2()));

		int status = doctor.getCertificationStatus();
		String sStatus;
		if(status == 0){
			sStatus = "未认证";
		}else if(status == 1){
			sStatus = "认证中";
		}else if(status == 2){
			sStatus = "已认证";
		}else if(status == 3){
			sStatus = "认证失败";
		}else{
			sStatus = null;
		}
		
		vo.setFaceStatus(sStatus);
		vo.setCertificationStatus(doctor.getCertificationStatus());
		return vo;
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param doctorCertification
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, DoctorCertification doctorCertification) {
		return super.exportXls(request, doctorCertification, DoctorCertification.class, "doctor_certification");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, DoctorCertification.class);
	}
}
